package com.wind.taobao.api.trade.user.convert
{
	import com.wind.taobao.api.trade.user.model.User;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * 用户转换器
	 * @author fxy
	 *
	 */
	public class UserConvert
	{
		/**
		 * 转换User集合
		 */
		public static function convertUsers(value:Object, type:String="json"):ArrayCollection
		{
			var users:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							users.addItem(convertUser((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return users;
		}

		/**
		 * 转换User
		 */
		public static function convertUser(value:Object, type:String="json"):User
		{
			var user:User=new User();
			if (value == null)
				return user;
			if (type == TaobaoConstants.XML)
			{
				user=convertXMLToUser(XML(value), user);
			}
			else
			{
				user=convertJSONToUser(value, user);
			}
			return user;
		}

		/**
		 * JSON转换为User
		 */
		private static function convertJSONToUser(json:Object, user:User):User
		{
			if (json.hasOwnProperty(TaobaoConstants.USER_ID))
			{
				user.user_id=json[TaobaoConstants.USER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.NICK))
			{
				user.nick=json[TaobaoConstants.NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.SEX))
			{
				user.sex=json[TaobaoConstants.SEX];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				user.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.LAST_VISIT))
			{
				user.last_visit=json[TaobaoConstants.LAST_VISIT];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_CREDIT))
			{
				user.buyer_credit=UserCreditConvert.convertUserCredit(json[TaobaoConstants.BUYER_CREDIT]);
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_CREDIT))
			{
				user.seller_credit=UserCreditConvert.convertUserCredit(json[TaobaoConstants.SELLER_CREDIT]);
			}
			if (json.hasOwnProperty(TaobaoConstants.LOCATION))
			{
				user.location=LocationConvert.convertLocation(json[TaobaoConstants.LOCATION]);
			}

			//隐私字段
			if (json.hasOwnProperty(TaobaoConstants.BIRTHDAY))
			{
				user.birthday=json[TaobaoConstants.BIRTHDAY];
			}
			if (json.hasOwnProperty(TaobaoConstants.TYPE))
			{
				user.type=json[TaobaoConstants.TYPE];
			}
			if (json.hasOwnProperty(TaobaoConstants.HAS_MORE_PIC))
			{
				user.has_more_pic=json[TaobaoConstants.HAS_MORE_PIC];//已转换Boolean
			}
			if (json.hasOwnProperty(TaobaoConstants.ITEMIMG_SIZE))
			{
				user.item_img_size=json[TaobaoConstants.ITEMIMG_SIZE];
			}
			if (json.hasOwnProperty(TaobaoConstants.ITEMIMG_NUM))
			{
				user.item_img_num=json[TaobaoConstants.ITEMIMG_NUM];
			}
			if (json.hasOwnProperty(TaobaoConstants.PROP_IMG_SIZE))
			{
				user.prop_img_size=json[TaobaoConstants.PROP_IMG_SIZE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PROP_IMG_NUM))
			{
				user.prop_img_num=json[TaobaoConstants.PROP_IMG_NUM];
			}
			if (json.hasOwnProperty(TaobaoConstants.AUTO_REPOST))
			{
				user.auto_repost=json[TaobaoConstants.AUTO_REPOST];
			}
			if (json.hasOwnProperty(TaobaoConstants.USER_PROMOTEDTYPE))
			{
				user.promoted_type=json[TaobaoConstants.USER_PROMOTEDTYPE];
			}
			if (json.hasOwnProperty(TaobaoConstants.USER_STATUS))
			{
				user.status=json[TaobaoConstants.USER_STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.USER_ALIPAYBAND))
			{
				user.alipay_bind=json[TaobaoConstants.USER_ALIPAYBAND];
			}
			if (json.hasOwnProperty(TaobaoConstants.CONSUMER_PROTECTION))
			{
				user.consumer_protection=json[TaobaoConstants.CONSUMER_PROTECTION];
			}
			return user;
		}

		/**
		 * XML转换为User
		 */
		private static function convertXMLToUser(value:XML, user:User):User
		{
			return user;
		}

	}
}